#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1e3 + 5;
const int W = 5e6 + 5;

int n, m;
int root[N];
int tr[W][26], sz[W], tot;
int newnode() { return ++tot; }
void insert(int& rt, string word) {
  if (!rt) rt = newnode();
  int p = rt;
  for (char c : word) {
    int x = c - 'a';
    if (!tr[p][x]) tr[p][x] = newnode();
    p = tr[p][x];
  }
  sz[p] = 1;
}
bool search(int rt, string word) {
  int p = rt;
  for (char c : word) {
    int x = c - 'a';
    if (!tr[p][x]) return false;
    p = tr[p][x];
  }
  return sz[p];
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) {
    cin >> m;
    rep(j, 1, m) {
      string w;
      cin >> w;
      insert(root[i], w);
    }
  }
  cin >> m;
  while (m--) {
    string w;
    cin >> w;
    rep(i, 1, n) if (search(root[i], w)) cout << i << ' ';
    cout << endl;
  }
  return 0;
}